
import { _getuser, _getcaptch } from '@/api/api';
import { _selectStationLabel,_selectStationForm } from '@/api/infoapi';
import { _selectTradeList,_selectProjectList } from '@/api/train';
import {router} from "umi"
import cookie from 'react-cookies'
interface Istate{
    title:string,
    token:string,
    uuid:string
}

export default{
    namespace:"app",
    state:{
        title:"我是标题",
        token:"",
        uuid:"",
        code:"",
        selectStationLabel:[],//岗位
        selectStationForm:[],
        selectTradeList: [],//实训项目
        selectProjectList:[]
    },
    reducers:{
        // updatetitle(state: Istate,{action}: any){
        //     return{
        //         ...state,
        //         title:action
        //     }
        // }   
        updateuuid(state: Istate,{action}: any){
            // console.log(action);
           console.log(state);
           
            
            return{
                ...state,
                uuid:action
            }
        },
        getuser(state: Istate,{action}: any){
            return{
                ...state,
                token:action.token
            }
        },
        getfilterlist(state: Istate,{action}: any){
            // console.log(action,"action");
            
            return{
                ...state,
                selectStationLabel:action
            }
        },
        getformlist(state: Istate,{action}: any){
            console.log(action,"action");
            
            return{
                ...state,
                selectStationForm:action
            }
        },
        gettrainlist(state: Istate,{action}: any){
            console.log(action,"train");
            
            return{
                ...state,
                selectTradeList:action
            }
        },
        getprojectlist(state: Istate,{action}: any){
            console.log(action,"train");
            
            return{
                ...state,
                selectProjectList:action
            }
        },

       

    },
    effects:{
        *getlogin({action}: any,{put,call}: any){
            console.log(action);
            
            const result = yield call(_getuser, action);
            console.log(result,6666);
            
           if(result.data.code==200){
            yield put({type:"getuser",action:result.data.token})
            //    window.localStorage.token=result.data.token
            let inFifteenMinutes = new Date(new Date().getTime() +  2*24*3600 * 1000);//一天
            
            cookie.save('token', result.data.token,{expires:inFifteenMinutes})
                router.push("/")
           }else{
               console.log("登录失败");
               
           }
           
        },
        //获取岗位筛选数据
        *selectStationLabel({action}:any,{call,put}:any){
            // console.log(action);    
            const result = yield call(_selectStationLabel)
            // console.log(result,"列表");
            yield put ({type:"getfilterlist",action:result.data.data})
   
        },
        //获取岗位表格数据
        *selectStationForm({action}:any,{call,put}:any){
            // console.log(action,12344546456);
            if(!action){
                const result = yield call(_selectStationForm)
                // console.log(result,"表格");
                yield put ({type:"getformlist",action:result.data.rows})
            }else{
                yield put ({type:"getformlist",action})
            }
            
        },
        //获取培训
        *selectTradeList({ action }: any, { call, put }: any) {
            console.log(action,"培训数据");
            
            if(!action){
                const result = yield call(_selectTradeList);
                console.log(result.data.data, '培训1');
                yield put({ type: 'gettrainlist', action: result.data.data });
            }else{
                 yield put ({type:"gettrainlist",action})
            }
            
              
           
          },
          *selectProjectList({ action }: any, { call, put }: any) {
            console.log(action,"培训表格数据");
            // console.log(typeof(action));
            
                if(!Array.isArray(action)){
                    const result = yield call(_selectProjectList,action);
                    console.log(result.data.rows, '培训2');
                    yield put({ type: 'getprojectlist', action:result.data.rows });
                }else{
                    console.log(333333333);
                    
                    yield put({ type: 'getprojectlist', action });
                }
                
           
            
              
           
          },
       
    }
}